//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    "bnrUrl": null,
    moduleList: null,
    "showDialog": false,
    "kids": [],
    letter:'2'
  },

  mokuaibuttton: function (e) {
    var index = e.currentTarget.dataset.index;
    var moduleCode=this.data.moduleList[index].ModuleCode
    console.log(moduleCode)
    if (moduleCode == 'new') {
      wx.navigateTo({
        url: '../news/news'
      })
    } else if (moduleCode == 'homework') {
      wx.navigateTo({
        url: '../homework/homework'
      })
    } else if (moduleCode == 'notify') {
      wx.navigateTo({
        url: '../notify/notify'
      })
    } else if (moduleCode == 'cookbook') {
      wx.navigateTo({
        url: '../cookbook/cookbook'
      })
    } else if (moduleCode == 'teacherinfo') {
      wx.navigateTo({
        url: '../teacher/teacher'
      })
    } else if (moduleCode == 'health') {
      wx.navigateTo({
        url: '../childhealth/childhealth'
      })
    } else if (moduleCode == 'studentbook') {
      wx.navigateTo({
        url: '../weekplan/weekplan'
      })
    } else if (moduleCode == 'movie') {
      wx.navigateTo({
        url: '../childonline/childonline'
      })
    }
    else if(moduleCode == 'mail'){
      wx.navigateTo({
        url: '../yuanzhangemail/yuanzhangemail'
      })
    }
    else if(moduleCode == 'archives'){
      wx.navigateTo({
        url: '../babymessage/babymessage'
      })
    }
    else if(moduleCode == 'myattendance'){
      wx.navigateTo({
        url: '../teacherattendance/teacherattendance'
      })
    }
    else if(moduleCode == 'mealreports'){
      wx.navigateTo({
        url: '../jiucanrenshu/jiucanrenshu'
      })
    }
    else if(moduleCode == 'meal'){
      wx.navigateTo({
        url: '../jiucanhuibao/jiucanhuibao'
      })
    }
    else if(moduleCode == 'kidattendance'){
      wx.navigateTo({
        url: '../childattendanceone/childattendanceone'
      })
    }else if (moduleCode == 'drug') {
      wx.navigateTo({
        url: '../fuyaoguanli/fuyaoguanli'
      })
    }
    else{
      wx.showModal({
        title: '提示',
        content: '此模块暂未开放',
      })
    }

  },
  onLoad: function () {
    var classid = wx.getStorageSync('classid');
    var classname = wx.getStorageSync('classname');
    app.globalData.classid = classid; //家长id
    app.globalData.classname = classname; //学校id
    console.log("userId==" + app.globalData.userId)
    console.log("schoolId==" + app.globalData.schoolId)
    console.log("classid==" + app.globalData.classid)
    console.log("classname==" + app.globalData.classname)
    this.getData();
  },

  /*点击变色*/
  click: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this
    that.setData({
      id: id
    })
  },

  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var index = e.detail.value;
    var that = this
    that.setData({
      value: e.detail.value
    })
    app.globalData.userId = this.data.kids[index].FamilyID
    app.globalData.schoolId = this.data.kids[index].SchoolID
    app.globalData.kidId = this.data.kids[index].UserID
    app.globalData.isMainContact = this.data.kids[index].IsMainContact
    console.log("userId==" + app.globalData.userId)
    console.log("schoolId==" + app.globalData.schoolId)
    console.log("kidId==" + app.globalData.kidId)
    console.log("isMainContact==" + app.globalData.isMainContact)
    console.log(this.data.value)
    wx.setStorageSync('userId', app.globalData.userId);
    wx.setStorageSync('kid', this.data.kids[index]);
    wx.setStorageSync('schoolId', app.globalData.schoolId);
    wx.setStorageSync('kidId', app.globalData.kidId);
    wx.setStorageSync('isMainContact', app.globalData.isMainContact);
  },
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  freeBack: function () {
    var that = this
    if (this.data.value == 'show') {
      wx.showModal({
        title: '提示',
        content: '你没有选择任何内容',
      })
    }
    that.setData({
      showDialog: !this.data.showDialog
    })
  },
  freetoBack: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '你没有选择任何内容',
    })
    that.setData({
      showDialog: !this.data.showDialog,
      value: 'show',
      checked: false,
    })
  },


  getData: function () {
    var that = this;
    var mobile = wx.getStorageSync('mobile'); //存储用户名
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobile": mobile,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_homepage"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {


          var list=res.data.data.moduleList;
          var list1=[];
          list.forEach(function(item, index){
            console.log(item); //这里的item就是从数组里拿出来的每一个每一组
            var moduleCode=item.ModuleCode
            if (moduleCode == 'new') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_news.png"
              list1.push(item);
            } else if (moduleCode == 'homework') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_homework.png"
              list1.push(item);
            } else if (moduleCode == 'notify') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_xiaoyuantongzhi.png"
              list1.push(item);
            } else if (moduleCode == 'archives') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_document.png"
              list1.push(item);
            } 
            else if (moduleCode == 'cookbook') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_meizhoushipu.png"
              list1.push(item);
            } else if (moduleCode == 'myattendance') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_teacher_attendance.png"
              list1.push(item);
            } else if (moduleCode == 'meal') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_eating.png"
              list1.push(item);
            } else if (moduleCode == 'mail') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_email.png"
              list1.push(item);
            } 
            else if (moduleCode == 'kidattendance') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_kaoqinjiesong.png"
              list1.push(item);
            } else if (moduleCode == 'studentbook') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_week_plan.png"
              list1.push(item);
            } else if (moduleCode == 'movie') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_babyonline.png"
              list1.push(item);
            }else if (moduleCode == 'mealreports') {
              list[ index ].ModuleKidgardenUrl="../../images/icon_jiucanrenshu.png"
              list1.push(item);
            }else if (moduleCode == 'drug') {
              list[index].ModuleKidgardenUrl = "../../images/icon_mediceine.png"
              list1.push(item);
            }
          })
          that.setData({
            bnrUrl: res.data.data.adList,
            moduleList:list1
          })

        } else {
          wx.showToast({
            title: "获取幼儿园信息失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

  },

})